package com.topc.platform.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;

import com.topc.platform.mapper.SignInInfoMapper;
import com.topc.platform.model.po.SignInInfo;
import com.topc.platform.service.ISignInInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class SignInInfoServiceImpl extends ServiceImpl<SignInInfoMapper, SignInInfo> implements ISignInInfoService {

    @Override
    public PageResult<SignInInfo> searchInfoByPage(PageParams pageParams) {
        Page page = this.page(pageParams.page());
        return PageUtils.pageToResult(page);
    }

    @Override
    public List<String> getSignInActionByIds(List<Long> ids) {
        List<SignInInfo> signInInfos = this.getBaseMapper().selectBatchIds(ids);
        List<String> collect = signInInfos.stream().map(SignInInfo::getAction).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Long> getIdsByRandom() {
        LambdaQueryWrapper<SignInInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SignInInfo::getId);
        List<SignInInfo> list = this.list(queryWrapper);
        List<Integer> indexs = new ArrayList<>();
        while (indexs.size()<4){
            for (int i = 0; i < 4 || indexs.size()==4; i++) {
                indexs.add(RandomUtil.randomInt(0, list.size()-1));
            }
            indexs = indexs.stream().distinct().collect(Collectors.toList());
        }
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < indexs.size(); i++){
            ids.add(list.get(indexs.get(i)).getId());
        }
        return ids;
    }


}
